package com.jweb.core.handle;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jweb.util.FormatUtil;

/**
 * 控制信息 Controller<br>
 * 该类为单例，使用全局变量会发送线程安全问题。请使用ThreadLoal定义变量
 */
public abstract class Controller {
	protected ThreadLocal<HttpServletRequest> request = new ThreadLocal<HttpServletRequest>();
	protected ThreadLocal<HttpServletResponse> response = new ThreadLocal<HttpServletResponse>();
	// (get)URL参数，通过下标取值
	private ThreadLocal<List<String>> parameterURL = new ThreadLocal<List<String>>();
	// (post)Form参数，通过Key取值
	private ThreadLocal<Map<String, String>> parameterMap = new ThreadLocal<Map<String, String>>();

	private ThreadLocal<Integer> returnType = new ThreadLocal<Integer>();
	private ThreadLocal<String> returnView = new ThreadLocal<String>();
	private ThreadLocal<String> defaultView = new ThreadLocal<String>(); // ==Action.viewPath

	public void load(HttpServletRequest request, HttpServletResponse response, List<String> ParamList, Map<String, String> parameterMap,
			String defaultView) {
		this.request.set(request);
		this.response.set(response);
		this.parameterURL.set(ParamList);
		this.parameterMap.set(parameterMap);
		this.defaultView.set(defaultView);
		this.returnType.set(0);
		this.returnView.set("");
	}

	// ==页面处理===============================
	/**
	 * 请求转发
	 * 
	 * @param view
	 */
	protected void toforward(String view) {
		returnType.set(1);
		returnView.set(view);
	}

	/**
	 * 请求重定向
	 * 
	 * @param view
	 */
	protected void toRedirect(String view) {
		returnType.set(2);
		returnView.set(view);
	}

	/**
	 * 向前端输出json字符串
	 * 
	 * @param str
	 */
	protected void toWriterJSON(Object str) {
		returnType.set(3);
		returnView.set(JSON.toJSONString(str, SerializerFeature.WriteDateUseDateFormat));
	}

	/**
	 * 向前端输出文本内容
	 * 
	 * @param str
	 */
	protected void toWriterText(String str) {
		returnType.set(4);
		returnView.set(str);
	}

	/**
	 * 向前端输出Html/xml内容
	 * 
	 * @param str
	 */
	protected void toWriterHtml(String str) {
		returnType.set(5);
		returnView.set(str);
	}

	// ==参数获取GET===============================
	/**
	 * 获取resful参数
	 */
	protected List<String> getParameterURL() {
		return parameterURL.get();
	}

	/**
	 * 获取resful参数
	 */
	public int getParamURL_int(int index) {
		return getParamURL_int(index, 0);
	}

	/**
	 * 获取resful参数
	 */
	public int getParamURL_int(int index, int def) {
		return FormatUtil.toInt(getParamURL_String(index, def + ""));
	}

	/**
	 * 获取resful参数
	 */
	public String getParamURL_String(int index) {
		return getParamURL_String(index, "");
	}

	/**
	 * 获取resful参数
	 */
	public String getParamURL_String(int index, String def) {
		if (getParameterURL().size() > index)
			return FormatUtil.toString(getParameterURL().get(index));
		else
			return def;
	}

	// ==参数获取POST===============================
	/**
	 * 获取请求参数
	 */
	protected String getParameter(String name) {
		return request.get().getParameter(name);
	}

	/**
	 * 获取请求参数
	 */
	protected Map<String, String> getParameterMap() {
		return parameterMap.get();
	}

	// ====================================
	public int getReturntype() {
		return returnType.get();
	}

	public String getReturnView() {
		return returnView.get();
	}

	public String getDefaultView() {
		return defaultView.get();
	}

	// ================================================
	/**
	 * 同request.setAttribute(name, value)
	 * @param name
	 * @param value
	 */
	protected void setAttr(String name, Object value) {
		this.request.get().setAttribute(name, value);
	}

	/**
	 * 同request.getAttribute(name)
	 * @param name
	 * @return
	 */
	protected Object getAttr(String name) {
		return this.request.get().getAttribute(name);
	}

}
